package com.cn.qgx428.orderingmeal.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.myClass.DimOrderInfo;
import com.cn.qgx428.orderingmeal.myClass.Food;
import com.cn.qgx428.orderingmeal.viewHolder.RViewHolder;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * Created by QinX on 2017/5/23.
 */

public class OrderAdapter extends RBaseAdapter {

    private Context context;

    private OnOrderItemOnclickListener orderItemOnclickListener;

    public interface OnOrderItemOnclickListener{
        void onClick(int position);
    }

    public OrderAdapter(Context context, List list, int itemLayoutId) {
        super(context, list, itemLayoutId);
        this.context = context;
    }

    @Override
    public void convert(final RViewHolder holder, Object item) {
        DimOrderInfo order= (DimOrderInfo) item;

        LinearLayout orderLayout = holder.getView(R.id.order_orderItemLayout);
        if (orderItemOnclickListener != null){
            orderLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    orderItemOnclickListener.onClick(position);
                }
            });
        }

        TextView orderNo = holder.getView(R.id.order_orderNo);
        TextView orderTime = holder.getView(R.id.order_time);
        TextView totalPrice = holder.getView(R.id.order_totalPrice);
        TextView totalAmout = holder.getView(R.id.order_totalAmount);
        ImageView pic = holder.getView(R.id.order_pic);

        orderNo.setText("编号："+order.getOrderNo());
        orderTime.setText(order.getOrderTime().substring(5,10));
        totalPrice.setText("￥"+String.valueOf(order.getTotalPrice()));
        totalAmout.setText("总共"+order.getTotalAmount()+"商品");
        setDimFoodInfo( pic, order);
    }

    private void setDimFoodInfo( final ImageView pic, final DimOrderInfo order) {
        //传入数据时ID暂时已存在name处，食物单种暂时已存在食物单种数量上
        BmobQuery<Food> query = new BmobQuery<Food>();
        query.getObject(order.getFoodName(), new QueryListener<Food>() {
            @Override
            public void done(Food food, BmobException e) {
                if ( e == null){
                    Picasso.with(context).load(food.getPic().getFileUrl()).fit().into(pic);
                }
            }
        });
    }

    public void setOrderItemOnclickListener(OnOrderItemOnclickListener orderItemOnclickListener){
        this.orderItemOnclickListener = orderItemOnclickListener;
    }
}
